% 2. Representation = 2vs6
% 2-2-4. Quality of Deliberation = 0.25 | Quantity of Deliberation = 108

clear
DS = zeros(1,10000);
Vr = zeros(1,10000);
Am = zeros(1,10000);

for k = 1:10000
    % 1) Setup
    m1 = zeros(12,12);
    for i = 1:18
        m1(randsample(144,1))=abs(normrnd(0,1))*-1;
    end
    m1;
    m2 = zeros(12,12);
    for i = 1:18
        m2(randsample(144,1))=abs(normrnd(0,1))*-1;
    end
    m2;
    M1 = zeros(12,12);
    for i = 1:18
        M1(randsample(144,1))=abs(normrnd(0,1));
    end
    M1;
    M2 = zeros(12,12);
    for i = 1:18
        M2(randsample(144,1))=abs(normrnd(0,1));
    end
    M2;
    M3 = zeros(12,12);
    for i = 1:18
        M3(randsample(144,1))=abs(normrnd(0,1));
    end
    M3;
    M4 = zeros(12,12);
    for i = 1:18
        M4(randsample(144,1))=abs(normrnd(0,1));
    end
    M4;
    M5 = zeros(12,12);
    for i = 1:18
        M5(randsample(144,1))=abs(normrnd(0,1));
    end
    M5;
    M6 = zeros(12,12);
    for i = 1:18
        M6(randsample(144,1))=abs(normrnd(0,1));
    end
    M6;

    % 2) Deliberation
    % (1) Random selection of a speaker S
    for j = 1:108
        r = rand;
        if r < 0.125 
        S = m1;
    elseif r < 0.25
        S = m2;
    elseif r < 0.375
        S = M1;
    elseif r < 0.5
        S = M2;
    elseif r < 0.625
        S = M3;
    elseif r < 0.75
        S = M4;
    elseif r < 0.875
        S = M5;
    else 
        S = M6;
    end
    % (2) Random selection of a non-zero information point
    info = randsample(nonzeros(S),1);
    % (3) Information sharing and learning
    index = find(S==info);
    if S == m1
        disp('nothing happens')
    elseif S == m2
        m1(index)=m1(index)+0.5*info;
    else 
        m1(index)=m1(index)+0.25*info;
    end
    if S == m2
        disp('nothing happens')
    elseif S == m1
        m2(index)=m2(index)+0.5*info;
    else
        m2(index)=m2(index)+0.25*info;
    end
    if S == M1
        disp('nothing happens')
    elseif S == m1 
        M1(index)=M1(index)+0.25*info;
    elseif S == m2
        M1(index)=M1(index)+0.25*info;
    else 
        M1(index)=M1(index)+0.5*info;
    end
    if S == M2
        disp('nothing happens')
    elseif S == m1
        M2(index)=M2(index)+0.25*info;
    elseif S == m2
        M2(index)=M2(index)+0.25*info;
    else 
        M2(index)=M2(index)+0.5*info;
    end
    if S == M3
        disp('nothing happens')
    elseif S == m1
        M3(index)=M3(index)+0.25*info;
    elseif S == m2
        M3(index)=M3(index)+0.25*info;
    else 
        M3(index)=M3(index)+0.5*info;
    end
    if S == M4
        disp('nothing happens')
    elseif S == m1
        M4(index)=M4(index)+0.25*info;
    elseif S == m2
        M4(index)=M4(index)+0.25*info;
    else 
        M4(index)=M4(index)+0.5*info;
    end
    if S == M5
        disp('nothing happens')
    elseif S == m1
        M5(index)=M5(index)+0.25*info;
    elseif S == m2
        M5(index)=M5(index)+0.25*info;
    else 
        M5(index)=M5(index)+0.5*info;
    end
    if S == M6
        disp('nothing happens')
    elseif S == m1
        M6(index)=M6(index)+0.25*info;
    elseif S == m2
        M6(index)=M6(index)+0.25*info;
    else 
        M6(index)=M6(index)+0.5*info;
    end
    end

    % 3) Voting
    % (1) Generating policy proposal
    PP = ones(1,12);
    m11 = sum(m1(:,1));
    m21 = sum(m2(:,1));
    M11 = sum(M1(:,1));
    M21 = sum(M2(:,1));
    M31 = sum(M3(:,1));
    M41 = sum(M4(:,1));
    M51 = sum(M5(:,1));
    M61 = sum(M6(:,1));
    PD1 = [m11, m21, M11, M21, M31, M41, M51, M61];
    if sum(PD1>0) >= 6
        PP(1,1) = 1;
    else 
        PP(1,1) = -1;
    end
    m12 = sum(m1(:,2));
    m22 = sum(m2(:,2));
    M12 = sum(M1(:,2));
    M22 = sum(M2(:,2));
    M32 = sum(M3(:,2));
    M42 = sum(M4(:,2));
    M52 = sum(M5(:,2));
    M62 = sum(M6(:,2));
    PD2 = [m12, m22, M12, M22, M32, M42, M52, M62];
    if sum(PD2>0) >= 6
        PP(1,2) = 1;
    else 
        PP(1,2) = -1;
    end
    m13 = sum(m1(:,3));
    m23 = sum(m2(:,3));
    M13 = sum(M1(:,3));
    M23 = sum(M2(:,3));
    M33 = sum(M3(:,3));
    M43 = sum(M4(:,3));
    M53 = sum(M5(:,3));
    M63 = sum(M6(:,3));
    PD3 = [m13, m23, M13, M23, M33, M43, M53, M63];
    if sum(PD3>0) >= 6
        PP(1,3) = 1;
    else 
        PP(1,3) = -1;
    end
    m14 = sum(m1(:,4));
    m24 = sum(m2(:,4));
    M14 = sum(M1(:,4));
    M24 = sum(M2(:,4));
    M34 = sum(M3(:,4));
    M44 = sum(M4(:,4));
    M54 = sum(M5(:,4));
    M64 = sum(M6(:,4));
    PD4 = [m14, m24, M14, M24, M34, M44, M54, M64];
    if sum(PD4>0) >= 6
        PP(1,4) = 1;
    else 
        PP(1,4) = -1;
    end
    m15 = sum(m1(:,5));
    m25 = sum(m2(:,5));
    M15 = sum(M1(:,5));
    M25 = sum(M2(:,5));
    M35 = sum(M3(:,5));
    M45 = sum(M4(:,5));
    M55 = sum(M5(:,5));
    M65 = sum(M6(:,5));
    PD5 = [m15, m25, M15, M25, M35, M45, M55, M65];
    if sum(PD5>0) >= 6
        PP(1,5) = 1;
    else 
        PP(1,5) = -1;
    end
    m16 = sum(m1(:,6));
    m26 = sum(m2(:,6));
    M16 = sum(M1(:,6));
    M26 = sum(M2(:,6));
    M36 = sum(M3(:,6));
    M46 = sum(M4(:,6));
    M56 = sum(M5(:,6));
    M66 = sum(M6(:,6));
    PD6 = [m16, m26, M16, M26, M36, M46, M56, M66];
    if sum(PD6>0) >= 6
        PP(1,6) = 1;
    else 
        PP(1,6) = -1;
    end
    m17 = sum(m1(:,7));
    m27 = sum(m2(:,7));
    M17 = sum(M1(:,7));
    M27 = sum(M2(:,7));
    M37 = sum(M3(:,7));
    M47 = sum(M4(:,7));
    M57 = sum(M5(:,7));
    M67 = sum(M6(:,7));
    PD7 = [m17, m27, M17, M27, M37, M47, M57, M67];
    if sum(PD7>0) >= 6
        PP(1,7) = 1;
    else 
        PP(1,7) = -1;
    end
    m18 = sum(m1(:,8));
    m28 = sum(m2(:,8));
    M18 = sum(M1(:,8));
    M28 = sum(M2(:,8));
    M38 = sum(M3(:,8));
    M48 = sum(M4(:,8));
    M58 = sum(M5(:,8));
    M68 = sum(M6(:,8));
    PD8 = [m18, m28, M18, M28, M38, M48, M58, M68];
    if sum(PD8>0) >= 6
        PP(1,8) = 1;
    else 
        PP(1,8) = -1;
    end
    m19 = sum(m1(:,9));
    m29 = sum(m2(:,9));
    M19 = sum(M1(:,9));
    M29 = sum(M2(:,9));
    M39 = sum(M3(:,9));
    M49 = sum(M4(:,9));
    M59 = sum(M5(:,9));
    M69 = sum(M6(:,9));
    PD9 = [m19, m29, M19, M29, M39, M49, M59, M69];
    if sum(PD9>0) >= 6
        PP(1,9) = 1;
    else 
        PP(1,9) = -1;
    end
    m110 = sum(m1(:,10));
    m210 = sum(m2(:,10));
    M110 = sum(M1(:,10));
    M210 = sum(M2(:,10));
    M310 = sum(M3(:,10));
    M410 = sum(M4(:,10));
    M510 = sum(M5(:,10));
    M610 = sum(M6(:,10));
    PD10 = [m110, m210, M110, M210, M310, M410, M510, M610];
    if sum(PD10>0) >= 6
        PP(1,10) = 1;
    else 
        PP(1,10) = -1;
    end
    m111 = sum(m1(:,11));
    m211 = sum(m2(:,11));
    M111 = sum(M1(:,11));
    M211 = sum(M2(:,11));
    M311 = sum(M3(:,11));
    M411 = sum(M4(:,11));
    M511 = sum(M5(:,11));
    M611 = sum(M6(:,11));
    PD11 = [m111, m211, M111, M211, M311, M411, M511, M611];
    if sum(PD11>0) >= 6
        PP(1,11) = 1;
    else 
        PP(1,11) = -1;
    end
    m112 = sum(m1(:,12));
    m212 = sum(m2(:,12));
    M112 = sum(M1(:,12));
    M212 = sum(M2(:,12));
    M312 = sum(M3(:,12));
    M412 = sum(M4(:,12));
    M512 = sum(M5(:,12));
    M612 = sum(M6(:,12));
    PD12 = [m112, m212, M112, M212, M312, M412, M512, M612];
    if sum(PD12>0) >= 6
        PP(1,12) = 1;
    else 
        PP(1,12) = -1;
    end
    PP;
    % (2) Acceptability of policy proposal
    Am1 = m11*PP(1,1)+m12*PP(1,2)+m13*PP(1,3)+m14*PP(1,4)+m15*PP(1,5)+m16*PP(1,6)+m17*PP(1,7)+m18*PP(1,8)+m19*PP(1,9)+m110*PP(1,10)+m111*PP(1,11)+m112*PP(1,12);
    Am2 = m21*PP(1,1)+m22*PP(1,2)+m23*PP(1,3)+m24*PP(1,4)+m25*PP(1,5)+m26*PP(1,6)+m27*PP(1,7)+m28*PP(1,8)+m29*PP(1,9)+m210*PP(1,10)+m211*PP(1,11)+m212*PP(1,12);
    AM1 = M11*PP(1,1)+M12*PP(1,2)+M13*PP(1,3)+M14*PP(1,4)+M15*PP(1,5)+M16*PP(1,6)+M17*PP(1,7)+M18*PP(1,8)+M19*PP(1,9)+M110*PP(1,10)+M111*PP(1,11)+M112*PP(1,12);
    AM2 = M21*PP(1,1)+M22*PP(1,2)+M23*PP(1,3)+M24*PP(1,4)+M25*PP(1,5)+M26*PP(1,6)+M27*PP(1,7)+M28*PP(1,8)+M29*PP(1,9)+M210*PP(1,10)+M211*PP(1,11)+M212*PP(1,12);
    AM3 = M31*PP(1,1)+M32*PP(1,2)+M33*PP(1,3)+M34*PP(1,4)+M35*PP(1,5)+M36*PP(1,6)+M37*PP(1,7)+M38*PP(1,8)+M39*PP(1,9)+M310*PP(1,10)+M311*PP(1,11)+M312*PP(1,12);
    AM4 = M41*PP(1,1)+M42*PP(1,2)+M43*PP(1,3)+M44*PP(1,4)+M45*PP(1,5)+M46*PP(1,6)+M47*PP(1,7)+M48*PP(1,8)+M49*PP(1,9)+M410*PP(1,10)+M411*PP(1,11)+M412*PP(1,12);
    AM5 = M51*PP(1,1)+M52*PP(1,2)+M53*PP(1,3)+M54*PP(1,4)+M55*PP(1,5)+M56*PP(1,6)+M57*PP(1,7)+M58*PP(1,8)+M59*PP(1,9)+M510*PP(1,10)+M511*PP(1,11)+M512*PP(1,12);
    AM6 = M61*PP(1,1)+M62*PP(1,2)+M63*PP(1,3)+M64*PP(1,4)+M65*PP(1,5)+M66*PP(1,6)+M67*PP(1,7)+M68*PP(1,8)+M69*PP(1,9)+M610*PP(1,10)+M611*PP(1,11)+M612*PP(1,12);

    % (3) Consensus decision rule
    if Am1>=0 && Am2>=0 && AM1>=0 && AM2>=0 && AM3>=0 && AM4>=0 && AM5>=0 && AM6>=0
        DS(1,k) = 1;
    else 
        DS(1,k) = 0;
    end
    
    % 4) Dependent variables
    Vr(1,k) = sum(PP<0)/12;
    if DS(1,k) == 1
        VrxDS(1,k) = Vr(1,k)*100;
    else
        VrxDS(1,k) = NaN;
    end

    Am(1,k) = (sum(sum(Am1))+sum(sum(Am2)))/12;
    if DS(1,k) == 1
        AmxDS(1,k) = Am(1,k);
    else
        AmxDS(1,k) = NaN;
    end

    AM(1,k) = (sum(AM1)+sum(AM2)+sum(AM3)+sum(AM4)+sum(AM5)+sum(AM6))/12;
    if DS(1,k) == 1
        AMxDS(1,k) = AM(1,k);
    else
        AMxDS(1,k) = NaN;
    end
end

writematrix(VrxDS','Vrd2t4.xls')

writematrix(AmxDS','Amd2t4.xls')

RAd = AmxDS./AMxDS;
writematrix(RAd','RAd2t4.xls')

mean(DS)*100
nanmean(VrxDS)
nanstd(VrxDS)
nanmean(AmxDS)
nanstd(AmxDS)
nanmean(RAd)*100
nanstd(RAd)
nanstd(RAd)*100